package by.grsu.bso.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import by.grsu.bso.form.ShoppingCartPutForm;
import by.grsu.bso.model.ShoppingCartItem;
import by.grsu.bso.model.User;
import by.grsu.bso.service.ServiceManager;

public class ShoppingCartPutAction extends Action {

	private final static String SUCCESS = "success";
	private final static String FAILURE = "failure";

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ShoppingCartPutForm putForm = (ShoppingCartPutForm) form;

		int productId = putForm.getProductId();
		int userId;

		HttpSession session = request.getSession(true);
		User user = (User) session.getAttribute("user");
		userId = user.getId();

		if (ServiceManager.getShoppingCartItemService().isSame(userId,productId) == true) {
			return mapping.findForward(FAILURE);
		} 

			ServiceManager.getShoppingCartItemService().saveItem(new ShoppingCartItem(userId, productId, 1));
			return mapping.findForward(SUCCESS);

		
	}

}
